﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPhotoAlbum.DbAccess;
using System.Globalization;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum.My
{
    public partial class Home : AlbumPage
	{
		#region Fields
		private const string ID_CREATING = "ltrCreating";
		private const string ID_ALBUM_THUMBNAIL = "hlAlbum";
        private const string ID_ALBUM_TITLE = "lblTitle";
        private const string ID_ALBUM_DESCRIPTION = "lblDescription";
        private const string ID_ALBUM_CREATION_DATE = "lblCreationDate";
        private const string ID_ALBUM_LAST_MODIFICATION_DATE = "lblLastModificationDate";
		private const string ID_ALBUM_DELETE = "linkBtnDelete";
        private const string ID_ALBUM_TITLE_TOOLTIP = "ttTitle";
        private const string ID_ALBUM_DESCRIPTION_TOOLTIP = "ttDescription";
		private const string ID_ALBUM_DELETE_TOOLTIP = "ttDelete";
		private Guid _newlyCreatedAlbum = Guid.Empty;
		private const string CMD_DELETE = "del";
		private int _currentPage = 0;
		#endregion

		protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

			this.Title = StringUtility.Format(Resources.Strings.Title_My_Home, HttpContext.Current.User.Identity.Name);

			tabPanels.TabPanelItems.Add(new TabPanelItem() {
				Name = Resources.Strings.My_CreateNewAlbum, PanelClientId = pnlCreateNewAlbum.ClientID, Visible = false });
			tabPanels.TabPanelItems.Add(new TabPanelItem() {
				Name = Resources.Strings.SharingAddress, PanelClientId = pnlSharingAddress.ClientID, Visible = false });
			string sharingAddress = UrlContext.BuildSharedHomeUrl(this.User.Identity.Name);
			ltrSharingAddress.Text = HttpUtility.HtmlEncode(Links.ToFullPath(sharingAddress));
			hlVisit.Text = Resources.Strings.VisitThisAddress;
			hlVisit.NavigateUrl = sharingAddress;
			btnCreateAlbum.Text = Resources.Strings.Create;
			btnCreateAlbum.Click += new EventHandler(btnCreateAlbum_Click);
			ltrAlbumTitle.Text = string.Format(CultureInfo.CurrentCulture, "{0}{1}", Resources.Strings.Title, Resources.Strings.Colon);
            rptAlbums.ItemDataBound += new RepeaterItemEventHandler(rptAlbums_ItemDataBound);
			rptAlbums.ItemCommand += new RepeaterCommandEventHandler(rptAlbums_ItemCommand);
        }

		protected void btnCreateAlbum_Click(object sender, EventArgs e)
		{
			_newlyCreatedAlbum = BusinessLogics.AlbumManager.Instance.CreateNewAlbum(
				this.UserId,
				tbAlbumTitle.Text,
				Resources.Strings.My_Album_DefaultDescription);

			LoadAlbums();
		}

		protected override void AppendSiteMapNodes(AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			provider.AppendSiteMapNode(Resources.Strings.SC_Home, Links.Default);
			provider.AppendSiteMapNode(Resources.Strings.My_Home, RawUrl);
		}

		protected void rptAlbums_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
            if (string.IsNullOrEmpty(e.CommandName) || e.CommandArgument == null)
            {
                return;
            }

            if (e.CommandName == CMD_DELETE)
            {
                Guid albumId = new Guid(e.CommandArgument.ToString());
                BusinessLogics.AlbumManager.Instance.DeleteAlbum(albumId);

                LoadAlbums();
            }
		}

        protected void rptAlbums_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
			DbAccess.Album album = e.Item.DataItem as DbAccess.Album;
            if (album == null) { return; }

			HyperLink hlAlbum = (HyperLink)e.Item.FindControl(ID_ALBUM_THUMBNAIL);
			InplaceEditLabel title = (InplaceEditLabel)e.Item.FindControl(ID_ALBUM_TITLE);
			InplaceEditLabel description = (InplaceEditLabel)e.Item.FindControl(ID_ALBUM_DESCRIPTION);
            Label creationDate = (Label)e.Item.FindControl(ID_ALBUM_CREATION_DATE);
            Label lastModificationDate = (Label)e.Item.FindControl(ID_ALBUM_LAST_MODIFICATION_DATE);
			AlbumLinkButton linkBtnDelete = (AlbumLinkButton)e.Item.FindControl(ID_ALBUM_DELETE);
            TooltipExtender ttTitle = (TooltipExtender)e.Item.FindControl(ID_ALBUM_TITLE_TOOLTIP);
            TooltipExtender ttDescription = (TooltipExtender)e.Item.FindControl(ID_ALBUM_DESCRIPTION_TOOLTIP);
			TooltipExtender ttDelete = (TooltipExtender)e.Item.FindControl(ID_ALBUM_DELETE_TOOLTIP);

			hlAlbum.NavigateUrl = UrlContext.BuildMyAlbumUrl(_currentPage, album.AlbumId, album.AlbumTitle, 0);
			Guid photoId = BusinessLogics.AlbumManager.Instance.GetAlbumCover(album.AlbumId);
			hlAlbum.ImageUrl = UrlContext.BuildPhotoPipeUrl(photoId, BusinessLogics.PhotoTypes.AlbumCover);
            title.Text = album.AlbumTitle;
			title.ContextData1 = "title";
			title.ContextData2 = album.AlbumId.ToString();
			title.ContextData3 = lastModificationDate.ClientID + "|" + hlAlbum.ClientID;
            title.TooltipBehaviorId = ttTitle.BehaviorID;
			//if (_newlyCreatedAlbum.Equals(album.AlbumId)) { title.IsEditing = true; }
            description.Text = album.AlbumDescription;
			description.ContextData1 = "description";
			description.ContextData2 = album.AlbumId.ToString();
			description.ContextData3 = lastModificationDate.ClientID;
			description.IsMultiLine = true;
            description.TooltipBehaviorId = ttDescription.BehaviorID;
			if (_newlyCreatedAlbum.Equals(album.AlbumId)) { description.IsEditing = true; }
            creationDate.Text = string.Format(
                CultureInfo.CurrentCulture,
                Resources.Strings.My_Album_CreationDate,
                album.AlbumCreationDate.ToShortDateString());
			if (album.AlbumModificationDate.HasValue)
			{
				lastModificationDate.Text = string.Format(
					CultureInfo.CurrentCulture,
					Resources.Strings.My_Album_LastModificationDate,
					album.AlbumModificationDate.Value.ToShortDateString());
			}
			else
			{
				lastModificationDate.Text = string.Empty;
			}
			linkBtnDelete.Text = Resources.Strings.Delete;
			linkBtnDelete.CommandName = CMD_DELETE;
			linkBtnDelete.CommandArgument = album.AlbumId.ToString();
			linkBtnDelete.OnClientClick = StringUtility.GetConfirmJs(Resources.Strings.My_Confirm_DeleteAlbum);
            ttTitle.Text = Resources.Strings.Tooltip_ClickToEdit;
			ttTitle.ParentElementId = pnlTooltipParent.ClientID;
            ttDescription.Text = Resources.Strings.Tooltip_ClickToEdit;
			ttDescription.ParentElementId = pnlTooltipParent.ClientID;
			ttDelete.Text = Resources.Strings.Tooltip_DeleteAlbum;
			ttDelete.ParentElementId = pnlTooltipParent.ClientID;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            LoadAlbums();
        }

        private void LoadAlbums()
        {
            rptAlbums.DataSource = BusinessLogics.AlbumManager.Instance.GetAlbums(this.UserId);
            rptAlbums.DataBind();
        }
    }
}
